package com.sinog.core.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @desc 汉字转拼音(用于根据中文生成账号,不要删除)
 * @author lj
 * @date 2021/12/9 13:51
 */
@Slf4j
public class PinYinUtils {

    /**
     * 汉字转全部拼音
     * @param chinaStr 汉字
     * @return String
     */
    public static String getAllPinyin(String chinaStr) {

        //输出格式设置
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        /*
         * 输出大小写设置
         * LOWERCASE:输出小写
         * UPPERCASE:输出大写
         */
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        /*
         * 输出音标设置
         * WITH_TONE_MARK:直接用音标符（必须设置WITH_U_UNICODE，否则会抛出异常）
         * WITH_TONE_NUMBER：1-4数字表示音标
         * WITHOUT_TONE：没有音标
         */
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        /*
            特殊音标ü设置
            WITH_V：用v表示ü
            WITH_U_AND_COLON：用"u:"表示ü
            WITH_U_UNICODE：直接用ü
         */
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        char[] hanYuArr = chinaStr.trim().toCharArray();
        StringBuilder pinYin = new StringBuilder();
        try {
            for(char c : hanYuArr) {
                //匹配是否是汉字
                if(Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    //如果是多音字，返回多个拼音，这里只取第一个
                    String[] pys = PinyinHelper.toHanyuPinyinStringArray(c,format);
                    pinYin.append(pys[0]).append(" ");
                } else {
                    pinYin.append(c).append(" ");
                }
            }
        } catch(BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            log.error(badHanyuPinyinOutputFormatCombination.getMessage());
        }
        return pinYin.toString();
    }

    /**
     * 汉字转拼音取首字母
     * @param chinaStr 汉字字符串
     * @return String
     */
    public static String getFirstSpell(String chinaStr) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = chinaStr.trim().toCharArray();
        HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
        pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for(char aChar : chars) {
            if(aChar > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(aChar,pyFormat);
                    if(temp != null) {
                        stringBuilder.append(temp[0].charAt(0));
                    }
                } catch(BadHanyuPinyinOutputFormatCombination e) {
                    log.error(e.getMessage());
                }
            } else {
                stringBuilder.append(aChar);
            }
        }
        return stringBuilder.toString();
    }
}